<template>
  <div class="box">
    <div class="card-container">
      <el-row :gutter="10">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
          label-width="80px">
          <el-col :span="19" :offset="0">
            <el-form-item label="厂商简称" prop="shortName">
              <el-input v-model="queryParams.shortName" placeholder="请输入厂商简称" clearable size="small"
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="厂商代码" prop="code">
              <el-input v-model="queryParams.code" placeholder="请输入厂商代码" clearable size="small"
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="是否启用" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
                <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
                  :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5" :offset="0">
            <el-form-item>
              <el-button  icon="el-icon-search" size="mini" @click="handleQuery">搜索
              </el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </div>

    <div class="card-container">
      <div class="body-title">
        <div class="body-title-left">
          厂商管理
        </div>
        <div class="body-title-right">
          <!-- <el-button  size="mini" :disabled="single" v-show="!multiple" @click="handleUpdate">
            修改</el-button> -->
          <el-button  size="mini" :disabled="multiple" v-hasPermi="['manufacturer:remove']" v-show="!multiple"
            @click="handleDelete">删除</el-button>
          <!-- <el-button  size="mini" v-show="!multiple" @click="handleExport">导出</el-button> -->
          <el-button   icon="el-icon-plus" size="mini" v-hasPermi="['manufacturer:add']" @click="handleAdd">新增
          </el-button>
        </div>
      </div>
      <el-table v-loading="loading" height="500" :data="manufacturerList" ref="table" :row-key="getRowKeys" border
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" :selectable="checkSelectable" :reserve-selection="true" align="center" />
        <el-table-column label="厂商全称" show-overflow-tooltip align="center" prop="name" />
        <el-table-column label="厂商简称" show-overflow-tooltip align="center" prop="shortName" />
        <el-table-column label="厂商代码" show-overflow-tooltip align="center" prop="code" />
        <el-table-column label="联系人" show-overflow-tooltip align="center" prop="userName" />
        <el-table-column label="联系电话" show-overflow-tooltip align="center" prop="phone" />
        <el-table-column label="管理归属" show-overflow-tooltip align="center" prop="deptName" />
        <el-table-column label="是否启用" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" v-hasPermi="['manufacturer:edit']" @click="handleUpdate(scope.row)">编辑</el-button>
            <!-- <el-button size="mini" type="text" icon="el-icon-delete"
              @click="handleDelete(scope.row)" v-hasPermi="['manufacturer:remove']">删除
            </el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>

    <!-- 添加或修改厂商管理对话框 -->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="500px"
      append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="厂商全称" prop="name">
          <el-input v-model="form.name" maxlength="20" placeholder="请输入厂商全称" />
        </el-form-item>
        <el-form-item label="厂商简称" prop="shortName">
          <el-input v-model="form.shortName" maxlength="10" placeholder="请输入厂商简称" />
        </el-form-item>
        <el-form-item label="厂商代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入厂商代码" />
        </el-form-item>
        <el-form-item label="联系人" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="管理归属" prop="deptId">
          <!-- <el-input v-model="form.deptId" placeholder="请输入管理归属" /> -->
          <el-cascader v-model="form.deptId" style="width:380px" clearable
            :props='{value:"id",label: "label",children:"children", emitPath:false}'
            :options="deptDate" @change="handleChange">
          </el-cascader>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
              :label="dict.value">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button  @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listManufacturer,
  getManufacturer,
  delManufacturer,
  addManufacturer,
  updateManufacturer,
} from '@/api/manufacturer/manufacturer';
import { getDept } from '@/api/common.js';

export default {
  name: 'Manufacturer',
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 厂商管理表格数据
      manufacturerList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        shortName: null,
        name: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phone: [{ validator: this.validatorPhone, trigger: 'blur' }],
        shortName: [
          { required: true, message: '厂商简称不能为空', trigger: 'blur' },
        ],
        code: [
          { required: true, message: '厂商代码不能为空', trigger: 'blur' },
        ],
      },
      deptDate: [],
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    checkSelectable(row){
      return row.useCount <= 0
    },
    // async strFun(row) {
    //   const res = await getDepts(row.deptId);
    //   if (res.code == 200) {
    //     return res.data.deptName;
    //   } else {
    //     return '--';
    //   }
    // },
    getRowKeys(row) {
      return row.facId;
    },
    handleChange() {},
    validatorPhone(rule, value, callback) {
      if (value !== null) {
        if (!/^1\d{10}$/.test(value)) {
          callback(new Error('请输入正确的联系电话'));
        } else {
          callback();
        }
      } else {
        callback();
      }
    },
    /** 查询厂商管理列表 */
    getList() {
      this.loading = true;
      listManufacturer(this.queryParams).then((response) => {
        this.manufacturerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        facId: null,
        code: null,
        shortName: null,
        name: null,
        userName: null,
        phone: null,
        deptId: null,
        remark: null,
        status: '0',
        delFlag: null,
        delTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.facId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    async handleAdd() {
      this.reset();
      const res = await getDept();
      if (res.code == 200) {
        this.deptDate = res.data;
      }
      this.open = true;
      this.title = '新增';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const facId = row.facId || this.ids;
      getManufacturer(facId).then(async (response) => {
        this.form = response.data;
        const res = await getDept();
        if (res.code == 200) {
          this.deptDate = res.data;
        }
        this.open = true;
        this.title = '编辑';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.facId != null) {
            updateManufacturer(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addManufacturer(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const facIds = row.facId || this.ids;
      this.$modal
        .confirm('是否确认删除当前厂商？')
        .then(function () {
          return delManufacturer(facIds);
        })
        .then(() => {
          this.$refs.table.clearSelection()
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'manufacturer/manufacturer/export',
        {
          ...this.queryParams,
        },
        `manufacturer_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  height: 100%;
  padding: 20px;
}
.body-title {
  display: flex;
  position: relative;
  // justify-content: space-between;
  align-items: center;
}
.body-title-left {
  height: 100%;
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 600;
  color: #fff;
  line-height: 64px;
}
.body-title-right {
  position: absolute;
  right: 0;
}
</style>


